package com.developer.t24haber.util;

import java.util.List;

import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.developer.t24haber.R;
import com.developer.t24haber.model.News;

public class NewsListAdapter extends BaseAdapter {

	private final List<News> news;
	private final ListFragment listFragment;

	public NewsListAdapter(List<News> news, ListFragment listFragment) {
		this.news = news;
		this.listFragment = listFragment;
	}

	@Override
	public int getCount() {
		return news.size();
	}

	@Override
	public Object getItem(int position) {
		return news.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		News currentNews = news.get(position);
		if (convertView == null) {
			convertView = listFragment.getActivity().getLayoutInflater().inflate(R.layout.list_view_news, parent, false);
		}
		TextView txtTitle = (TextView) convertView.findViewById(R.id.list_view_news_title);
		txtTitle.setText(currentNews.getTitle());
		ImageView imgNews = (ImageView) convertView.findViewById(R.id.list_view_news_image);
		imgNews.setImageBitmap(currentNews.getImage());
		return convertView;
	}
}
